﻿#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
#include <QPainter>
#include <QDebug>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    mtimer = new QTimer(this);
    connect(mtimer,&QTimer::timeout,[=](){
        startAngle += 15;
        if(startAngle >= 360){
            startAngle = 0;
        }
        update();
    });
    mtimer->setInterval(90);
    mtimer->start();
}

Widget::~Widget()
{
    delete ui;
}

void Widget::paintEvent(QPaintEvent *event)
{
    /* QPainter painter(this);
//    painter.setPen(Qt::blue);
    painter.setRenderHint(QPainter::Antialiasing,true);  //边缘光滑
    QPen pen(Qt::red,7);
    painter.setPen(pen);
    painter.setFont(QFont("Arial", 30));
    painter.drawText(rect(), Qt::AlignCenter, "Qt");
    //画圆
    painter.drawEllipse(QPoint(100,100),80,80);
    //画矩形    //位置，位置，长，宽
    painter.drawRect(200,200,100,200);
    //画线
    painter.drawLine(50,50,80,80);
    //画圆弧
    painter.drawArc(50,50,200,200,30*16,90*16);
    //画扇形
    painter.drawPie(100,100,200,200,30*16,90*16);*/

    //线性渐变
    /*QPainter painter(this);
    QLinearGradient lineGradient(width()/2,0,width()/2,height());
    lineGradient.setColorAt(0.1,QColor(255,255,255,255));  // 设置起始点，起始点的颜色
    lineGradient.setColorAt(1,QColor(0,0,0,255));// 设置结束点，结束点的颜色
    QBrush brush(lineGradient);
    painter.setBrush(brush);
    painter.drawRect(rect());*/
    // 径向渐变
    /* QPainter painter(this);
    QRadialGradient radialGradient(250,200,100); //中心点（X,Y） 半径
    radialGradient.setColorAt(0.1, QColor(255,255,255,255));
    radialGradient.setColorAt(1, QColor(0,0,0,255));
    painter.setBrush(QBrush(radialGradient));
    painter.drawRect(100,100,300,200);*/
    //圆锥渐变
    /* QPainter painter(this);
    QConicalGradient conicalGradient(250,200,0);//中心点（X,Y） 起始角度
    conicalGradient.setColorAt(0.1, Qt::red);
    conicalGradient.setColorAt(0.5, Qt::blue);
    conicalGradient.setColorAt(1, Qt::red);
    painter.setBrush(QBrush(conicalGradient));
    painter.setPen(Qt::NoPen);   //设置无边框显示
    painter.drawRect(100,100,300,200);*/

    // 扫描样式
    QPainter painter(this);
    //设置抗锯齿
    painter.setRenderHint(QPainter::Antialiasing,true);
    // 设置背景色为黑色
    QBrush brush(Qt::black);
    painter.setBrush(brush);
    painter.drawRect(rect());
    // 设置矩形的中心点为零点
    painter.translate(rect().center());
    //画圆
    int rEve = height()/2/7;
    //圆形半径
    int temData = rEve*7;
    QPen pen(QColor(0,255,0),4);
    painter.setPen(pen);
    painter.setBrush(Qt::NoBrush); //取消笔刷 防止新图形覆盖旧图形
    for(int i = 0;i<8;i++){
        painter.drawEllipse(QPoint(0,0),rEve*i,rEve*i);
    }
    // 画直线
    painter.drawLine(-temData,0,temData,0); // 横线
    painter.drawLine(0,-temData,0,temData); // 竖线
    //设置锥形渐变
    QConicalGradient conicalGradient(0,0,-startAngle);
    conicalGradient.setColorAt(0, QColor(0,255,0,200));
    conicalGradient.setColorAt(0.1, QColor(0,255,0,100));
    conicalGradient.setColorAt(0.2, QColor(0,255,0,0));
    conicalGradient.setColorAt(1, QColor(0,255,0,0));
    painter.setBrush(conicalGradient);
    painter.setPen(Qt::NoPen);
    painter.drawPie(QRect(-temData,-temData,temData*2,temData*2),-startAngle*16,70*16);

}

